package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import sparrowGui.skin.HScrollBarSkin;

	/**
	 * 水平普通滚动条
	 * 使用方法是先设置mask显示容器，再设置上下滚动的显示容器
	 * 在ScrollBar里面给所有点击移动事件加判断,水平和纵向再进行不同处理,还不如复制一个类出来做水平滚动条
	 * 因为水平条很少用到,加写在ScrollBar浪费CPU计算次数
	 * @author Pelephone
	 * 
	 */	
	public class HScrollBar extends Component
	{
		// 滚动条的宽度
		public static const THICKNESS:Number 	= 14;
		public static const MIN_SLIDER_HEIGHT:int = 5;
		
		private static const DELAY_MOVE_TIME:int = 500;	// 点击后延迟500毫秒，滚动条朝方向滚动
		
		// /是否自动隐藏和显示滚动条,ture一直显示，false内容高小于mask高时隐藏
		public var autoHide:Boolean;
		
		//被分隔成N分,上下箭头每点击一次silder上下的高度
		protected var segment:Number;
		
		// 0~1之间的数,表示被盖住的显示容器与遮照容器的坐标比
		private var _value:Number = 0;
		
		//用于暂存点击时间和Y坐标
		private var _clickTime:int;		// 暂存按下时的时间，外加enterframe判断点击了多长时间
		private var _clickPosi:Number;		// 暂存点击目标的位置
		private var _mouseLR:int;			// 暂存鼠标按下或者是放开事件数据
		
		public function HScrollBar(argSkin:Sprite=null)
		{
			_skin = argSkin || new HScrollBarSkin();
			super();
		}
		
		override protected function init():void
		{
			addChild(skin);
		}
		
/**		
		 * 设置进行滚动的两显示对象，使对象1庶照对象2进行滚动
		 * @param maskDP
		 * @param contDP
		 * @param step 每点击一下箭头滚动的距离
		 
		public function activeScroll(maskDP:DisplayObject,contDP:DisplayObject,step:Number=0):void
		{
			PubFun.alignRect(maskDP,contDP,"TL");
			contDP.mask = maskDP;
			
			if(sliderDist<leftBtn.width || maskDP.width>=contDP.width){
				enabled = false;
				delEvt();
				if(!autoHide) this.visible = false;
			}
			else
			{
				enabled = true;
//				slider.height = ScrollBar.THICKNESS;
				var slh:int = int(sliderDist*maskDP.width/contDP.width);
				slider.width = slh;
				if(slider.width<MIN_SLIDER_HEIGHT) slider.width = int(MIN_SLIDER_HEIGHT);
				addEvt();
				this.visible = true;
			}
			
			if(!step) segment = scrollDist/25;
			else
				segment = (sliderDist-slh)*step/(contDP.width-maskDP.width);
		}*/
		
		/**
		 * 通过要移动的对象的最小位置和最大位置初始激活滚动条
		 * 中间滚动块在最左边的时间value是min,最右边时value是max
		 * @param min 		滚动条value为0时对应容器的最小位置,Vlist的maskDP.width
		 * @param max 		滚动条value为1时对应容器的最大位置,Vlist的contDP.width
		 * @param stepValue 每点一次滚动率
		 */
		public function setSliderParams(min:Number, max:Number, stepValue:Number=0):void
		{
			if((leftBtn && sliderDist<leftBtn.width) || min>=max){
				enabled = false;
				delEvt();
				return;
			}
			else
			{
				enabled = true;
				var slh:int = int(sliderDist*min/max);
				slider.width = slh;
				if(slider.width<MIN_SLIDER_HEIGHT) slider.width = int(THICKNESS);
				addEvt();
				this.visible = true;
			}
			
//			var stepValue:Number = step/(max-min);
			
			if(!stepValue) segment = scrollDist/25;
			else
				segment = (sliderDist-slh)*stepValue;
		}
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		/**
		 * 添加事件
		 */
		protected function addEvt():void
		{
			delEvt();	//防止多次添加事件，先清除再添加
			slider.addEventListener(MouseEvent.MOUSE_DOWN, onSliderDown);
			this.addEventListener(MouseEvent.MOUSE_DOWN,onDownClick);
		}
		
		/**
		 * 删除事件
		 */		
		protected function delEvt():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onDownClick);
			slider.removeEventListener(MouseEvent.MOUSE_DOWN, onSliderDown);
		}
		
		protected function onDownClick(e:MouseEvent):void
		{
			_clickTime = getTimer();
			_clickPosi = e.localX*((e.target==skinbg)?skinbg.scaleX:1);
			if(e.target == rightBtn){
				_mouseLR = 0;
				onLRBtn();
			}
			else if(e.target == leftBtn)
			{
				_mouseLR = 1;
				onLRBtn();
			}
			else if(e.target == skinbg)
			{
				_mouseLR = 2
				onMove(null);
			}
			if(_mouseLR>2 || !stage) return;
			stage.addEventListener(Event.ENTER_FRAME,onMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,goUp);
			stage.addEventListener(Event.MOUSE_LEAVE,goUp);
//			onMoveSlider(null);
		}
		
		private function onLRBtn():void
		{
			if(!slider.visible){
				_value = _value - 0.05*(_mouseLR*2-1);
				onMoveSlider(null);
				return;
			}
			slider.x = slider.x - segment*(_mouseLR*2-1);
			if(!_mouseLR && (slider.x + segment)>sliderMaxX)
				slider.x = sliderMaxX;
			
			if(leftBtn){
				if(_mouseLR==1 && (slider.x - segment)<leftBtn.width)
					slider.x = leftBtn.width;
			}else
				if(_mouseLR==1 && (slider.x - segment)<skinbg.x)
					slider.x = leftBtn.width;
					
			
			onMoveSlider(null);
		}
		
		private function onMove(evt:Event):void
		{
			if(evt!=null && getTimer()<(_clickTime+DELAY_MOVE_TIME)) return;
			if(_mouseLR<2){
				onLRBtn();
				return;
			}
			if(_clickPosi>=slider.x && _clickPosi<=(slider.x + slider.width)){
				goUp(null);
				return;
			}
			if(_clickPosi>slider.x){
				var ex:int = slider.x + slider.width;
				var dis:int = style.width - slider.width - (leftBtn?leftBtn.width:0);
				if(ex>dis) ex = dis;
				slider.x = ex;
			}else{
				ex = slider.x - slider.width;
				if(leftBtn && ex<leftBtn.width) ex = leftBtn.width;
				if(!leftBtn && ex<skinbg.x) ex = skinbg.x;
				slider.x = ex;
			}
			onMoveSlider(null);
		}
		
		private var _sliderMouseX:int;	//暂存鼠标点击相对滚动条中轴位置
		protected function onSliderDown(e:MouseEvent):void
		{
			_mouseLR = 3;
			_sliderMouseX = e.localX*slider.scaleX;
//			var rect:Rectangle = new Rectangle(leftBtn.width,0,scrollDist,0);
//			slider.startDrag(false,rect);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMoveSlider);
			stage.addEventListener(MouseEvent.MOUSE_UP,goUp);
			stage.addEventListener(Event.MOUSE_LEAVE,goUp);
		}
		
		public function onMoveSlider(e:MouseEvent):void
		{
			if(e){
				var lx:int = leftBtn?(leftBtn.x+leftBtn.width):skinbg.x;
				var rx:int = rightBtn?(rightBtn.x - slider.width):(skinbg.width - slider.width);
				if((this.mouseX - _sliderMouseX)<lx)
					slider.x = lx;
				else if((this.mouseX - _sliderMouseX)>rx)
					slider.x = rx;
				else slider.x = this.mouseX - _sliderMouseX;
			}
			
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		protected function goUp(e:Event):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMoveSlider);
			stage.removeEventListener(MouseEvent.MOUSE_UP,goUp);
			stage.removeEventListener(Event.ENTER_FRAME,onMove);
			stage.removeEventListener(Event.MOUSE_LEAVE,goUp);
//			slider.stopDrag();
		}
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		override public function set enabled(value:Boolean):void
		{
			super.enabled = value;
			
			if(leftBtn)
				leftBtn.alpha = value ? 1.0 : 0.5;
			if(rightBtn)
				rightBtn.alpha = value ? 1.0 : 0.5;
			
			slider.visible = value;
		}
		
		/**
		 * 减掉上下箭头高的距离
		 */		
		protected function get sliderDist():int
		{
			if(skinbg && (!leftBtn || !rightBtn)) return style.width;
			return rightBtn.x - leftBtn.width - leftBtn.x;
		}
		
		/**
		 * 可滚动的距离
		 */		
		protected function get scrollDist():int
		{
			return sliderDist - slider.width;
		}
		
		/**
		 * slider的y坐标的比值
		 */		
		public function get value():Number
		{
			if(slider.visible){
				if(leftBtn)
					return (slider.x - leftBtn.width)/scrollDist;
				else
					return slider.x/scrollDist;
			}
			else
				return _value;
		}
		
		public function set value(v:Number):void
		{
			if(v>1) _value = 1;
			else if(v<0) _value = 0;
			else{
				_value = v;
			}
			var tx:int = leftBtn?leftBtn.width:skinbg.x;
			if(slider.visible) slider.x = tx + scrollDist*_value;
		}
		
		override protected function draw():void
		{
			super.draw();
			slider.x = scrollDist*_value + (leftBtn?leftBtn.width:0);
		}
		
		// 中条能滚动的最大Y坐标
		public function get sliderMaxX():Number
		{
			if(rightBtn)
				return rightBtn.x - slider.width;
			else
				return skinbg.x - slider.width;
		}
		
		override public function set width(value:Number):void
		{
//			skin.width	= value;
			if(leftBtn) leftBtn.x	= 0;
			if(rightBtn) rightBtn.x	= value - rightBtn.width;
			if(skinbg) skinbg.width = value;
			slider.x = scrollDist*_value+ (leftBtn?leftBtn.width:0);
			super.width = value;
		}
		
		override public function get width():Number
		{
			if(enabled) return style.width;
			else return 0;
		}
		
		
		override public function get height():Number
		{
			return style.height;
		}
		
/*		override public function dispose():void
		{
			super.destroy();
		}*/
		
		public function get leftBtn():DisplayObject{ return skin.getChildByName("leftBtn") as DisplayObject };
		public function get rightBtn():DisplayObject{ return skin.getChildByName("rightBtn") as DisplayObject };
		public function get slider():DisplayObject{ return skin.getChildByName("slider") as DisplayObject };
		public function get skinbg():DisplayObject{ return skin.getChildByName("skinbg") as DisplayObject };
	}
}